#include <QtGui>
#include <QtOpenGL>
#include <QDebug>
#include <iostream>

#include "Route.h"

Route::Route(const QVector<s_point3d>&dots, QWidget *parent)
    : QGLWidget(parent)
{
    setFormat(QGLFormat(QGL::DoubleBuffer | QGL::DepthBuffer));

    rotationX = -1.0;
    rotationY = -57.0;
    rotationZ = 0.0;

    m_dots = dots;
}

void Route::initializeGL()
{
    qglClearColor(Qt::black);
    glShadeModel(GL_FLAT);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
}

void Route::resizeGL(int width, int height)
{
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    GLfloat x = GLfloat(width) / height;
    glFrustum(-x, +x, -1.0, +1.0, 4.0, 15.0);
    glMatrixMode(GL_MODELVIEW);
}

void Route::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    draw();
}

void Route::mousePressEvent(QMouseEvent *event)
{
    lastPos = event->pos();
}

void Route::mouseMoveEvent(QMouseEvent *event)
{
    GLfloat dx = GLfloat(event->x() - lastPos.x()) / width();
    GLfloat dy = GLfloat(event->y() - lastPos.y()) / height();

    if (event->buttons() & Qt::LeftButton) {
        rotationX += 180 * dy;
        rotationY += 180 * dx;
        updateGL();
    } else if (event->buttons() & Qt::RightButton) {
        rotationX += 180 * dy;
        rotationZ += 180 * dx;
        updateGL();
    }
    lastPos = event->pos();
}

double Min_x(const QVector<s_point3d> &dots)
{
    double Min_x=dots[0].X;
    for(int q=0; q < dots.size(); ++q)
    {
       if (dots[q].X< Min_x)
          Min_x=dots[q].X;
    }

    return Min_x;

}

double Max_x(const QVector<s_point3d> &dots)
{
    double Max_x=dots[0].X;
    for(int q=0; q < dots.size(); ++q)
    {
       if (dots[q].X> Max_x)
          Max_x=dots[q].X;
    }

    return Max_x;

}

double Min_y(const QVector<s_point3d> &dots)
{
    double Min_y=dots[0].Y;
    for(int q=0; q < dots.size(); ++q)
    {
       if (dots[q].Y< Min_y)
          Min_y=dots[q].Y;
    }

    return Min_y;

}

double Max_y(const QVector<s_point3d> &dots)
{
    double Max_y=dots[0].Y;
    for(int q=0; q < dots.size(); ++q)
    {
       if (dots[q].Y> Max_y)
          Max_y=dots[q].Y;
    }

    return Max_y;

}
double Min_z(const QVector<s_point3d> &dots)
{
    double Min_z=dots[0].Z;
    for(int q=0; q < dots.size(); ++q)
    {
       if (dots[q].Z< Min_z)
          Min_z=dots[q].Z;
    }

    return Min_z;

}

double Max_z(const QVector<s_point3d> &dots)
{
    double Max_z=dots[0].Z;
    for(int q=0; q < dots.size(); ++q)
    {
       if (dots[q].Z> Max_z)
          Max_z=dots[q].Z;
    }

    return Max_z;

}
void Route::draw()
{

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0.0, 0.0, -10.0);
    glRotatef(rotationX, 1.0, 0.0, 0.0);
    glRotatef(rotationY, 0.0, 1.0, 0.0);
    glRotatef(rotationZ, 0.0, 0.0, 1.0);

    for(int q=0; q < m_dots.size(); ++q)
    {
      glPointSize(10);
      glBegin(GL_POINTS);
      glEnable(GL_POINT_SMOOTH);
      qglColor(Qt::red);
      glVertex3f(m_dots[q].X, m_dots[q].Y, m_dots[q].Z);

      glEnd();
    }
}





